<!DOCTYPE html>
<html lang="en">

<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
	<link rel="stylesheet" href="stylesheets/login.css">
	<script src="libs/axios.min.js"></script>
</head>

<body>
	<form name="userForm" method="post">
		<p>username: <input type="text" name="username" value="test" /></p>
		<p>password: <input type="text" name="password"
				value="8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92" /></p>
		<input type="submit" value="Submit" />
	</form>
	<button id="logout">logout-退出登录</button>
	<button id="view">view-显示</button>
	<button id="clear">clear-清除</button>
</body>
<script type="text/javascript">
	window.onload = function () {
		const instance = axios.create();
		instance.interceptors.response.use(function (response) {
			if (response.status == 200) {
				return response.data
			}
			return response.data;
		}, function (error, data) {
			if (error.response) {
				let { data, code, message } = error.response.data;
				if (code === 401) {
					alert(message)
					return location.href = `http://localhost:8000/login`
				}
			}
			return Promise.reject(error);
		});
		let urlParams = new URLSearchParams(window.location.search);
		let serviceURL = urlParams.get('serviceURL');
		instance({
			url: '/sso/user/doLogin',
			method: 'POST',
			data: {
				serviceURL,
			}
		}).then(function (response) {
			console.log(response)
			let { code, data, message } = response;
			data && (location.href = data)
		}).catch(function (error) {
			console.log(error)
		});

		document.getElementById('logout').onclick = async () => {
			instance({
				url: '/sso/user/logout',
				method: 'POST',
			}).then(function (response) {
				console.log('logout', response)
			}).catch(function (error) {
				console.log(error)
			}).finally(function () {
				// location.href = `http://localhost:8000/login?serviceURL=${location.href}`
			});
		}
		document.getElementById('view').onclick = () => {
			instance({
				url: '/sso/user/view',
				method: 'POST',
			}).then(function (response) {
				console.log('view', response)
			}).catch(function (error) {
				console.log(error)
			})
		}
		document.getElementById('clear').onclick = () => {
			instance({
				url: '/sso/user/clear',
				method: 'POST',
			}).then(function (response) {
				console.log('clear', response)
			}).catch(function (error) {
				console.log(error)
			})
		}

		// 登录
		var oForm = document.forms['userForm'];
		oForm.reset();
		oForm.onsubmit = function () {
			var username = this['username'].value;
			var password = this['password'].value;
			instance({
				url: '/sso/user/login',
				method: 'POST',
				data: {
					username,
					password,
					serviceURL,
				}
			}).then(function (response) {
				let { code, data, message } = response;
				location.href = data
			}).catch(function (error) {
				console.log(error)
			});
			return false;
		}
	}
</script>

</html>